home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / single2 / maintest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-07  |  4.4 KB  |  162 lines

  1. //  Listing 9
  2. //  maintest.cpp
  3. //  contains the code for the main test routines 
  4. //  for access to the global heap
  5. //
  6. //  Copyright Singleton Systems Ltd 1994
  7.  
  8. #include    "stdafx.h" 
  9. #include    "newtest.h" 
  10. #include    "mobject.h"
  11. #include    "farheapb.h"
  12. #include    <iostream.h> 
  13. #include    <fstream.h>
  14. #include    <iomanip.h>
  15. #include    "cmostr.h"
  16.  
  17. class FAR sort_node : public MemoryObject
  18. private:
  19. int value,              //  The number to sort on
  20.     original_order;     //  The order prior to sorting
  21. sort_node * less_equal, //  Pointer to sort_node tree
  22.                         //  holding sort-nodes with
  23.           * greater;    //  Pointer to sort_node tree
  24.                         //  holding sort-nodes with 
  25.                         //  value > this->value
  26. char filler [1000];     //  To make a sort_node bigger
  27.                         //  for test purposes
  28. public:  
  29. sort_node ();
  30. void read_node (ifstream &in);
  31.                         //  Reads a sort_node from <in>
  32. void enter_item (sort_node * item);
  33.                         //  Traverses a sort_node tree
  34.                         //  to insert item at the right
  35.                         //  point.
  36. void write_node (ofstream &out);  
  37.                         //  Writes the contents of a
  38.                         //  sort_node to <out>
  39. static sort_node * unlink_least_item 
  40.         (sort_node FAR * FAR * pp_branch);
  41.                         //  Traverses a sort_node
  42.                         //  branch, looking for the
  43.                         //  least item.  When the 
  44.                         //  least item is found, it is
  45.                         //  unlinked from the branch.
  46.                         //  Returns a pointer to the
  47.                         //  least item.
  48. };
  49.  
  50. sort_node::sort_node ()
  51.     {less_equal = greater = NULL;}
  52.  
  53. void sort_node::read_node (ifstream &file_in)
  54. {
  55. ASSERT (this != NULL);
  56. int item1, item2;
  57. file_in >> item1 >> item2; 
  58. value = item1; original_order = item2;
  59. }
  60.  
  61. void sort_node::write_node (ofstream &out)
  62. {
  63. #pragma warning (disable: 4270)
  64. //  stop unwanted warnings from iomanip.h
  65. ASSERT (this != NULL);
  66. out << dec << setw (10) << value << setw (10) 
  67.     << original_order << endl;
  68. #pragma warning (default: 4270)
  69. }
  70.  
  71. void sort_node::enter_item (sort_node * item)
  72. {
  73. if (item->value <= value)
  74.     {
  75.     if (less_equal == NULL)
  76.         less_equal = item;
  77.     else less_equal->enter_item (item);
  78.     }
  79. else
  80.     {
  81.     if (greater == NULL)
  82.         greater = item;
  83.     else greater->enter_item (item);
  84.     }   
  85. }
  86.  
  87. sort_node * sort_node::unlink_least_item 
  88.         (sort_node FAR * FAR * pp_branch)
  89. {
  90. if ((*pp_branch)->less_equal != NULL)
  91.     //  Search down the less_equal branch 
  92.     return unlink_least_item 
  93.                         (&((*pp_branch)->less_equal));
  94. else
  95.     {   //  This node is the least item, so unlink it
  96.     sort_node * current = * pp_branch;
  97.     * pp_branch = current->greater;
  98.     current->less_equal = current->greater = NULL;
  99.     return current;
  100.     }
  101. }
  102.  
  103. sort_node * tree = NULL; 
  104.  
  105. #define new MO_DEBUG_NEW
  106.  
  107. void CComdtestApp::DoTest ()
  108. {
  109. cout << "CComdtestApp::OnFileOpen () called" << endl;
  110.  
  111. CMOString * tmp_str = new CMOString ("some test text");
  112.     {
  113.     CMOString test = "abc";
  114.     test = test + "def"; 
  115.     cout << "test = " << test << endl;
  116.     }
  117.  
  118. void FAR * p_tmp = MemoryObject::AllocateBlock (21);
  119. MemoryObject::ReturnBlock (p_tmp); 
  120.  
  121. ifstream fin ("newtest.txt", ios::in | ios::nocreate);
  122. ASSERT (fin.is_open());
  123. int no_of_entries;
  124. fin >> no_of_entries; 
  125. ASSERT (no_of_entries >=1);
  126. tree = new sort_node;
  127. tree->read_node (fin);
  128. for (int i = 2; i <= no_of_entries; i++)
  129.     {
  130.     sort_node * item = new sort_node;
  131.     item->read_node (fin);
  132.     tree->enter_item (item);
  133.     }
  134.  
  135. CMOString * tmp_str2 = 
  136.                 new CMOString ("some more test text");
  137.  
  138. fin.close ();   
  139. }
  140.  
  141. void CComdtestApp::OnFileClose ()
  142. {
  143. //  We misuse the OnFileClose function to delete the 
  144. //  sort tree and to generate a heap report.  We do
  145. //  not really close anything.
  146. cout << "CComdtestApp::OnFileClose () called" << endl;
  147. if (tree != NULL)
  148.     {
  149.     ofstream fout ("newrpt.txt");           
  150.     while (tree != NULL)
  151.         {
  152.         sort_node * least_item = 
  153.                 sort_node::unlink_least_item (&tree);
  154.         least_item->write_node (fout);
  155.         delete least_item;
  156.         }
  157.     CComdtestApp::OnFilePrintHeapReport();    
  158.     fout.close();
  159.     }
  160. }
  161.